import React from 'react';
import { HashRouter as Router, Switch, Route, Redirect } from 'react-router-dom';
import AuthRoute from './layouts/AuthRoute';
import MainLayout from './layouts/main';
import List from './views/List';
import Handle from './views/Handle';
import Login from './views/Login';
import Result from "./views/Result";
import useUserInfo from './models/userInfo';

import logo from './logo.svg';
import './App.scss';

function App() {
  Date.prototype.Format = function (fmt) { //author: meizz
    let o = {
      "M+": this.getMonth() + 1, //月份
      "d+": this.getDate(), //日
      "h+": this.getHours(), //小时
      "m+": this.getMinutes(), //分
      "s+": this.getSeconds(), //秒
      "q+": Math.floor((this.getMonth() + 3) / 3), //季度
      "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  };

  const { userInfo } = useUserInfo();

  return (
    <MainLayout>
      <Router>
        <Switch>
          <Route exact path="/">
            {userInfo.uid && userInfo.uid !== 0 ? (
              <Redirect to="/list" />
            ) : (
              <Redirect to="/login" />
            )}
          </Route>
          <AuthRoute path="/list">
            <List />
          </AuthRoute>
          <AuthRoute path="/handle">
            <Handle />
          </AuthRoute>
          <Route path="/login" component={Login} />
          <Route path="/result" component={Result} />
          <AuthRoute>
            <header className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
              <p>404</p>
            </header>
          </AuthRoute>
        </Switch>
      </Router>
    </MainLayout>
  );
}

export default App;
